var app = getApp()

Page({
  data: {
    res:"",
    userName: app.globalData.userInfo,
    userPhone:"15910586524",
    layer:false,
    issign:true,
    signone:true,
    signtwo: true,
    signthree: true,
    signfore: true,
    signfive: true,
    signsix: true,
    signseven: true,
    SignInfo:'',
    UserBalance:'',
    myDou:'',
    myMoney:"",
    myDay:""
    
  },
  onLoad: function () {
    console.log("load")
    wx.showShareMenu({
      withShareTicket: true,
      success: function (res) {
        console.log('shareMenu share success')
      },
      fail: function (res) {

      }
    })
    this.setData({
      userName: app.globalData.userInfo
    })
    this.getUserSign()
    this.getUserBalance()
  },
  onShow: function () {
    this.onLoad();
  },
  onShareAppMessage: function () {
    return {
      title: '测试一下',
      path: '/pages/myCenter/myCenter',
      success: function (res) {
        console.log(res.shareTickets[0])
        wx.getShareInfo({
          shareTicket: res.shareTickets[0],
          success: function (res) { console.log(res) },
          fail: function (res) { console.log(res) },
          complete: function (res) { console.log(res) }
        })
      },
      fail: function (res) {
        console.log(res)
      }
    }
  },
  showLayer:function(){
    this.setData({
      layer:false,
    })
  },
  goActive:function(){ //好友红包页面
    wx.navigateTo({
      url: '../active/active',
    })
  },

  todownload: function () { //提现指导
    if (app.globalData.balance.money > 1){
      wx.navigateTo({
        url: '../../pages/downloadApp/downLoadApp',
      })
    }else{
      this.setData({
        layer:true
      })
    }
  },

  todownloadapp:function(){ 
    wx.navigateTo({
      url: '../../pages/downloadApp/downLoadApp',
    })
  },
  UserSign : function(){// 签到
    var that = this
    console.log(app.globalData.systemInfo)
    delete app.globalData.systemInfo.code
    delete app.globalData.systemInfo.encryptedData
    delete app.globalData.systemInfo.iv
    delete app.globalData.systemInfo.sign
    var dataObj = app.globalData.systemInfo;
    console.log(dataObj)
    dataObj.uid = app.globalData.userInfo.uid
    dataObj.channel="6666";
    var signurl = 'https://kandian.youth.cn:8181/WechatApp/User/sign.json?' + app.makeUrl(dataObj)
    wx:wx.request({
      url: signurl,
      data: {
        sign: app.makeSign(dataObj) 
      },
      success: function(res) {
        if (parseInt(res.data.error_code) != 0) {
          wx.showToast({
            title: res.data.message,
            duration: 1000,
          })
          //return false;
        }
        // 个人数据更新
        if (res.data.error_code==0){
          that.setData({
            myDay: res.data.item.num,
            myDou: parseInt(res.data.item.score) + parseInt(that.data.myDou),
            myMoney: res.data.item.money
          })
        }else{
          wx.showToast({
            title: res.data.message,
            duration:1000
          })
        }
        if (res.data.item.num == 1){
          that.setData({
            issign: false,
            signone: false,
          })
        }
        else if (res.data.item.num == 2) {
          that.setData({
            issign: false,
            signone: false,
            signtwo: false,
          })
        }
        else if (res.data.item.num == 3) {
          that.setData({
            issign: false,
            signone: false,
            signtwo: false,
            signthree: false,
          })
        }
        else if (res.data.item.num == 4) {
          that.setData({
            issign: false,
            signone: false,
            signtwo: false,
            signthree: false,
            signfore: false,
          })
        }
        else if (res.data.item.num == 5) {
          that.setData({
            issign: false,
            signone: false,
            signtwo: false,
            signthree: false,
            signfore: false,
            signfive: false,
          })
        }
        else if (res.data.item.num == 6) {
          that.setData({
            issign: false,
            signone: false,
            signtwo: false,
            signthree: false,
            signfore: false,
            signfive: false,
            signsix: false,
          })
        }
        else if (res.data.item.num >= 7) {
          that.setData({
            issign: false,
            signone: false,
            signtwo: false,
            signthree: false,
            signfore: false,
            signfive: false,
            signsix: false,
            signseven: false,
          })
        }
        console.log('0.0',res.data)
      },
    })
  },

  getUserSign : function(){  //获取用户签到状态
    var that = this
    app.globalData.allInfo.uid = app.globalData.userInfo.uid
    var signurl = 'https://kandian.youth.cn:8181/WechatApp/User/getSign.json?' + app.makeUrl(app.globalData.allInfo)
    wx: wx.request({
      url: signurl,
      data: {
        sign: app.makeSign(app.globalData.allInfo)
      },
      success: function (res) {
        console.log(2323232323)
        console.log('getSign', res.data)
        that.setData({
          myDay: parseInt(res.data.item.sing_num)
        })
        if (res.data.item.today == 0){
          that.setData({
            issign: true
          })
        }else{
          that.setData({
            issign: false
          })
        }
        if (res.data.item.sing_num == 1){
          that.setData({
            signone:false
          })
        }
        else if (res.data.item.sing_num == 2) {
          that.setData({
            signone: false,
            signtwo: false,
          })
        }
        else if (res.data.item.sing_num == 3) {
          that.setData({
            signone: false,
            signtwo: false,
            signthree: false,
          })
        }
        else if (res.data.item.sing_num == 4) {
          that.setData({
            signone: false,
            signtwo: false,
            signthree: false,
            signfore: false,
          })
        }
        else if (res.data.item.sing_num == 5) {
          that.setData({
            signone: false,
            signtwo: false,
            signthree: false,
            signfore: false,
            signfive: false,
          })
        }
        else if (res.data.item.sing_num == 6) {
          that.setData({
            signone: false,
            signtwo: false,
            signthree: false,
            signfore: false,
            signfive: false,
            signsix: false,
          })
        }
        else if (res.data.item.sing_num >= 7) {
          that.setData({
            signone: false,
            signtwo: false,
            signthree: false,
            signfore: false,
            signfive: false,
            signsix: false,
            signseven: false,
          })
        }
        that.setData({
          SignInfo:res.data.item
        })
      },
    })
  },

  SignIn:function(){  //用户签到点击
    this.UserSign()
  },

  getUserBalance :function(){
    var that = this
    delete app.globalData.systemInfo.code
    delete app.globalData.systemInfo.encryptedData
    delete app.globalData.systemInfo.iv
    delete app.globalData.systemInfo.sign
    var parameter = app.globalData.systemInfo;
    parameter.uid = app.globalData.userInfo.uid
    parameter.channel = "6666";
    parameter.sign = app.makeSign(parameter)
    wx.request({
      url: 'https://kandian.youth.cn:8181/WechatApp/Wechat/getUserInfo.json',
      data: parameter,
      method: 'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        console.log('userinfo',res)
        // 更新本地储存
        if(res.data.items){
          console.log("更新缓存")
          wx.setStorageSync("userData", res.data.items)
        }
        app.globalData.balance = res.data.items
        that.setData({
          UserBalance: res.data.items,
        })
        that.setData({
          myDou: res.data.items.today_score,
          myMoney: res.data.items.money
        })

      }
    })
  },
})